package com.ruoyi.utils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;


/**
 * @program: class by cxf
 * @author: AbrahamBill
 * @date: 2022/9/27
 **/
public class PicUtil {

    public static File multipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        if (fileName==null || fileName.equals("")) {
            return null;
        }
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            System.out.println("MultipartFile转换为File转换异常：" + e.getMessage());
        }
        return null;
    }
    /**
     * 纠正图片旋转
     *
     * @param srcImgPath
     */
    public static void correctImg(String srcImgPath) {
        FileOutputStream fos = null;
        try {
            // 原始图片
            File srcFile = new File(srcImgPath);

            // 获取偏转角度
            int angle = getAngle(srcFile);
//            if (angle != 90 && angle != 270) {
//                return;
//            }

            // 原始图片缓存
            BufferedImage srcImg = ImageIO.read(srcFile);

            // 宽高互换
//            // 原始宽度
//            int imgWidth = srcImg.getHeight();
//            // 原始高度
//            int imgHeight = srcImg.getWidth();
            int imgHeight = srcImg.getHeight();
            int imgWidth = srcImg.getWidth();
            // 中心点位置
            double centerWidth = ((double) imgWidth) / 2;
            double centerHeight = ((double) imgHeight) / 2;

            // 图片缓存
            BufferedImage targetImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);

            // 旋转对应角度
            Graphics2D g = targetImg.createGraphics();
            g.rotate(Math.toRadians(angle), centerWidth, centerHeight);
            g.drawImage(srcImg, (imgWidth - srcImg.getWidth()) / 2, (imgHeight - srcImg.getHeight()) / 2, null);
            g.rotate(Math.toRadians(-angle), centerWidth, centerHeight);
            g.dispose();

            // 输出图片
            fos = new FileOutputStream(srcFile);
            ImageIO.write(targetImg, "jpg", fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取图片旋转角度
     *
     * @param file 上传图片
     * @return
     */
    private static int getAngle(File file) throws Exception {
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                if ("Orientation".equals(tag.getTagName())) {
                    String orientation = tag.getDescription();
                    System.out.println(orientation);
                    if (orientation.contains("90")) {
                        return 90;
                    } else if (orientation.contains("180")) {
                        return 180;
                    } else if (orientation.contains("270")) {
                        return 270;
                    }
                }
            }
        }
        return 0;
    }


}
//import java.awt.Dimension;
//import java.awt.Graphics2D;
//import java.awt.Rectangle;
//import java.awt.RenderingHints;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.HashMap;
//import java.util.Map;
//import javax.imageio.ImageIO;
//import com.drew.imaging.ImageMetadataReader;
//import com.drew.imaging.ImageProcessingException;
//import com.drew.metadata.Directory;
//import com.drew.metadata.Metadata;
//import com.drew.metadata.Tag;
//import org.springframework.web.multipart.MultipartFile;
//
//public class PicUtil {
//    public static File multipartFileToFile(MultipartFile multiFile) {
//        // 获取文件名
//        String fileName = multiFile.getOriginalFilename();
//        if (fileName==null || fileName.equals("")) {
//            return null;
//        }
//        // 获取文件后缀
//        String prefix = fileName.substring(fileName.lastIndexOf("."));
//        try {
//            File file = File.createTempFile(fileName, prefix);
//            multiFile.transferTo(file);
//            return file;
//        } catch (Exception e) {
//            System.out.println("MultipartFile转换为File转换异常：" + e.getMessage());
//        }
//        return null;
//    }
//    public static Map<String, Object> getExif(InputStream inputStream) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
//            map = printExif(metadata);
//        } catch (ImageProcessingException e) {
//            System.out.println(e.getMessage());
//        } catch (IOException e) {
//            System.out.println(e.getMessage());
//        }
//        return map;
//    }
//    // 获取exif信息，将旋转角度信息拿到
//    private static Map<String, Object> printExif(Metadata metadata) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        String tagName = null;
//        String desc = null;
//        for (Directory directory : metadata.getDirectories()) {
//            for (Tag tag : directory.getTags()) {
//                tagName = tag.getTagName();
//                desc = tag.getDescription();
//                if (tagName.equals("Orientation")) {
//                    map.put("Orientation", desc);
//                }
//            }
//        }
//        return map;
//    }
//    public static int getAngle(Map<String, Object> map) {
//        int ro = 0;
//        if(map.get("Orientation")!=null) {
//            String ori = map.get("Orientation").toString();
//            if (ori.indexOf("90") >= 0) {
//                ro = 1;
//            } else if (ori.indexOf("180") >= 0) {
//                ro = 2;
//            } else if (ori.indexOf("270") >= 0) {
//                ro = 3;
//            }
//        }
//        return ro;
//    }
//    public static BufferedImage getBufferedImg(BufferedImage src,int width, int height, int ro) {
//        int angle = (int) (90 * ro);
//        int type = src.getColorModel().getTransparency();
//        int wid = width;
//        int hei = height;
//        if (ro % 2 != 0) {
//            int temp = width;
//            width = height;
//            height = temp;
//        }
//        Rectangle re = new Rectangle(new Dimension(width, height));
//        BufferedImage BfImg = null;
//        BfImg = new BufferedImage(re.width, re.height, type);
//        Graphics2D g2 = BfImg.createGraphics();
//        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//        g2.rotate(Math.toRadians(angle), re.width / 2,re.height / 2);
//        g2.drawImage(src, (re.width - wid) / 2, (re.height - hei) / 2, null);
//        g2.dispose();
//        return BfImg;
//    }
//    //获得图片的高
//    public static int getHeight(InputStream is) {
//        BufferedImage src = null;
//        int height = -1;
//        try {
//            src = ImageIO.read(is);
//            height = src.getHeight();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
//        return height;
//    }
//    //获得图片的宽
//    public static int getWidth(InputStream is) {
//        BufferedImage src = null;
//        int width = -1;
//        try {
//            src = ImageIO.read(is);
//            width = src.getWidth();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
//        return width;
//    }
//}